<?php
namespace app\admin\controller;

class Index extends Admin{
    public function __construct(){
        parent::__construct();
        $this->assign("model","admin");
    }
    public function index(){
        //第一级菜单
        $menus = model("Menu")->where("pid",0)->where('hide',0)->order("sort")->select();
        $menuarr = [];
        $menulist = [];
        foreach($menus as $vm){
            if(!checkAdminRule($vm['url'],$this->uinfo['roleid'])) continue;
            $pid = $vm['id'];
            $child_list = model("Menu")->where("pid",$pid)->where('hide',0)->order("sort")->select();
            $clist = [];
            foreach($child_list as $vs){
                if(!checkAdminRule($vs['url'],$this->uinfo['roleid'])) continue;
                $vs['active'] = 0;
                // if($vs['url'] == $url){
                //     $vs['active'] = 1;
                //     $active = 1;
                // }
                $clist[] = $vs;
            }
            // $vm['active'] = $active;
            $vm['child'] = $clist;
            $vm['ischild'] = empty($clist) ? 0 : 1;

            $menulist[] = $vm;

            // $groups = model('Menu')->where(array('group'=>array('neq',''),'pid' =>$pid))->where('hide', 0)->distinct(true)->field("group")->select();
            // $menulist = [];
            // foreach($groups as $v){
            //     $active = 0;
            //     $child_list = model("Menu")->where("pid",$pid)->where('hide',0)->where("group",$v['group'])->order("sort")->select();
            //     $clist = [];
            //     foreach($child_list as $vs){
            //         if(!checkAdminRule($vs['url'],$this->uinfo['roleid'])) continue;
            //         $vs['active'] = 0;
            //         // if($vs['url'] == $url){
            //         //     $vs['active'] = 1;
            //         //     $active = 1;
            //         // }
            //         $clist[] = $vs;
            //     }
            //     if(empty($clist)){
            //         continue;
            //     }
            //     $menulist[$v['group']]['active'] = $active;
            //     $menulist[$v['group']]['child'] = $clist;
            // }

            // $menuarr[] = array('title'=>$vm['title'],'child'=>$menulist);
        }
        $this->assign("menus",$menulist);
    	return $this->fetch();
    }
    public function main(){
        $list = model('MemberStatistics')->get_lists();
        $this->assign('list', $list);
        return $this->fetch();
    }
    public function show_child_menu(){
    	$groupid = input("param.groupid",0,"intval");
    	$menus = db("Menu")->where("group",$groupid)->order("sort")->select();
    	$this->assign("menus",$menus);
    	$content = $this->fetch();
    	return json(['status'=>1,'content'=>$content]);
    }
}